package com.nineclock.system.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.nineclock.common.constant.SMSConstant;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.sms.AliyunSmsUtils;
import com.nineclock.common.utils.PhoneNumCheckUtils;
import com.nineclock.system.service.SmsService;
import com.nineclock.system.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * 发送短信
 */
@Service
@Slf4j
public class SmsServiceImpl implements SmsService {

    @Autowired
    private SysUserService userService;

    @Autowired
    private AliyunSmsUtils smsUtils;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public void sendSms(String mobile, String type) {
        //参数校验
        if(StrUtil.isEmpty(mobile) || StrUtil.isEmpty(type)){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        //校验手机号
        if(!PhoneNumCheckUtils.isPhoneLegal(mobile)){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        //如果是注册, 要求手机号必须 不存在 ;
        if("2".equals(type)){//注册
            Boolean exists = userService.checkExists(mobile);
            if(exists){
                throw new NcException(ResponseEnum.USER_MOBILE_EXISTS);
            }
        }else{
            //如果是 登录/更换管理员/重置密码 操作, 手机号必须已注册存在
            Boolean exists = userService.checkExists(mobile);

            if(!exists){
                throw new NcException(ResponseEnum.USER_NOT_REGISTER);
            }
        }


        //生成随机数
        String code = RandomUtil.randomNumbers(6);

        //调用工具类发送验证码
        Boolean result = true; //smsUtils.sendSMS(mobile, code);
        if(!result){
            throw  new NcException(ResponseEnum.SEND_MESSAGE_ERROR);
        }

        log.info("生成验证码 , 手机号: "+ mobile + ", 验证码: "+ code);

        //将验证码存redis ----> 有效期5分钟
        String key = getPrefix(type)+mobile;
        redisTemplate.boundValueOps(key).set(code, 5 , TimeUnit.MINUTES);

    }


    /**
     * 获取验证码存储的key的前缀
     * @param type
     * @return
     */
    private String getPrefix(String type){
        String prefix = "";
        switch (type){
            case "1":
                prefix = SMSConstant.SMS_LOGIN_KEY_PREFIX;//登录
                break;
            case "2":
                prefix = SMSConstant.SMS_REGISTER_KEY_PREFIX;//注册
                break;
            case "3":
                prefix = SMSConstant.SMS_CHANGE_MANAGER_KEY_PREFIX;//更改管理员
                break;
            case "4":
                prefix = SMSConstant.SMS_RESET_KEY_PREFIX;//重置密码
                break;
            default:
                break;
        }
        return prefix;
    }



    @Override
    public Boolean verify(String checkcode, String mobile, String type) {
        //参数校验
        if(StrUtil.isEmpty(checkcode) || StrUtil.isEmpty(mobile) || StrUtil.isEmpty(type)){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        //组装key
        String key = getPrefix(type)+mobile;

        //获取redis中验证码
        String realCode = redisTemplate.boundValueOps(key).get();

        //对比
        if(StrUtil.isNotEmpty(realCode) && realCode.equals(checkcode)){
            return true;
        }

        return false;
    }


    @Override
    public void deleteCode(String mobile, String type) {
        String key = getPrefix(type)+mobile;
        redisTemplate.delete(key);
    }
}
